package com.taichuan.selfcheck.mvp.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.orhanobut.logger.Logger;
import com.taichuan.camera.TcCameraManager;
import com.taichuan.camera.TcTextureViewCameraUtils;
import com.taichuan.camera.entity.TcTakePhotoResult;
import com.taichuan.mvp.base.MvpBasePresenter;
import com.taichuan.selfcheck.SelfCheckAppModuleConfig;
import com.taichuan.selfcheck.config.PathConfig;
import com.taichuan.selfcheck.mvp.viewinterface.TestCameraInterface;
import com.taichuan.tcutils.common.BitmapUtil;
import com.taichuan.tcutils.common.FileUtils;
import com.taichuan.tcutils.common.TimeUtils;
import com.taichuan.tcutils.common.YuvToBitmapUtil;

import java.io.File;

/**
 * Created by CamelLuo on 2018/6/21.
 */
public class TestCameraPresenter extends MvpBasePresenter<TestCameraInterface> {
    private static final String TAG = TestCameraPresenter.class.getSimpleName();
    /**
     * YUV图像数据转Bitmap工具类
     */
    private YuvToBitmapUtil yuvToBitmapUtil;
    private Context context;

    public TestCameraPresenter(Context context) {
        this.context = context;
        initYuvToBitmapUtils();
    }

    @Override
    public void detachView() {
        super.detachView();
        destoryYuvToBitmapUtils();
    }

    private void initYuvToBitmapUtils() {
        //实例化YUV转Bitmap转换类
        yuvToBitmapUtil = YuvToBitmapUtil.getInstance();
        yuvToBitmapUtil.init(context);
    }

    public void destoryYuvToBitmapUtils(){
        if (yuvToBitmapUtil != null){
            yuvToBitmapUtil.destory();
        }
    }

    public void getTcCameraUtilsPreviewFrame() {
        TcCameraManager.get().getCurrentCameraUtilVideoFrame(false, new TcTextureViewCameraUtils.TcTakePhotoAsyncCallback() {
            @Override
            public void onPictureTaken(boolean isInUseCameraUtil,boolean isSucess, boolean isTimeout, TcTakePhotoResult tcTakePhotoResult) {
                if (isInUseCameraUtil){
                    if (tcTakePhotoResult != null){
                        Logger.t(TAG).e("获取到的抓拍数据为：  " + tcTakePhotoResult.toString());
                        //将获取到的视频帧数据转换为Bitmap
                        saveCameraYuvPhoto(tcTakePhotoResult.getImageWidth(), tcTakePhotoResult.getImageHeight(),
                                tcTakePhotoResult.getImageData());
                    }else {
                        Logger.t(TAG).e("获取到的抓拍数据为null：  ");
                    }
                }
            }
        });
    }

    private void saveCameraYuvPhoto(int imageWidth, int imageHeight, byte[] imageData) {
        if (imageData != null){
            //将YUV视频数据转换成Bitmap缓存下来
            Bitmap image = yuvToBitmapUtil.convertYuvToBitmap(imageData, imageWidth, imageHeight);
            //根据预览方向决定是否需要对Bitmap进行旋转操作
            if (SelfCheckAppModuleConfig.getCameraPreviewOration() != 0) {
                image = BitmapUtil.rotaingImageView(SelfCheckAppModuleConfig.getCameraPreviewOration(), image);
            }
            //通知外部组件显示Bitmap
            getView().refreshTakePhotoView(image);
        }

    }


    public void saveTakePhotoImage(int imageWidth, int imageHeight,byte[] imageData) {
        //将图像转换为File保存存起来
        File imageFile = new File(PathConfig.getTakePhotoBaseDirPath(),TimeUtils.getCurrentTime("yyyy-MM-dd_HH-mm-ss") + "_" + getView().getCameraTypeName() + ".jpeg");
        //判断File的父目录是否存在，如不存在则先创建
        FileUtils.checkFileAndMakeDirs(imageFile);
        //保存到指定路径
        boolean isSuccess = FileUtils.writeFile(imageFile,imageData);
        Log.i(TAG, "saveTakePhotoImage: 保存本次通过摄像头拍照结果：  " + isSuccess +
                "抓拍存储路径为： " + imageFile.getPath() + " 抓拍图像分辨率： " + imageWidth + " * " + imageHeight);
    }
}
